
package nl.ruudshuis.data;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import nl.ruudshuis.beans.GebruikerBean;
import nl.ruudshuis.beans.VeilingBean;
import nl.ruudshuis.dao.BiedingDao;
import nl.ruudshuis.dao.VeilingDao;
import nl.ruudshuis.db.DBmanager;

/**
 * Servlet implementation class VeilingController
 */
public class VeilingController extends HttpServlet {
	private static final long serialVersionUID = 1L;   
	private int counter = 10;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public VeilingController() {
        super();
    }

	/**
	 * 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		VeilingDao d = new VeilingDao((DBmanager) getServletContext().getAttribute("dbman"));
		BiedingDao b = new BiedingDao((DBmanager) getServletContext().getAttribute("dbman"));
		String item = request.getParameter("item");
		
		if(item != null){
			
			if(item.equals("toevoegen")){
				HttpSession session = request.getSession();
				
				if(session.getAttribute("loggedIn") != null){
					RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/toevoegen.jsp");
					view.forward(request, response);
				}
				else{
					response.sendRedirect("VeilingController");
				}
				
			}
			
			else if(item.equals("zoeken")){
				String zoek = request.getParameter("zoekopdracht");
				
				request.setAttribute("zoekVeilingen", d.zoekVeilingen(zoek));
				RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/zoek.jsp");
				view.forward(request, response);
			}
			
			else if(item.equals("meer")){
				counter += 10;
				request.setAttribute("veilingen", d.getVeilingen(counter));
				RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/refresh.jsp");
				view.forward(request, response);
			}
			
			else if(item.equals("refresh")){
				if(request.getParameter("page") != null){
					if(request.getParameter("page").equals("home")){
						request.setAttribute("veilingen", d.getVeilingen(counter));
						RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/refresh.jsp");
						view.forward(request, response);
					}
					if(request.getParameter("page").equals("mijnveiling")){
						HttpSession session = request.getSession();
						
						if(session.getAttribute("loggedIn") != null){
							GebruikerBean gb = (GebruikerBean) session.getAttribute("loggedIn");
							int userId = gb.getId();
						
							request.setAttribute("veilingen", d.getMijnVeilingen(userId));
							RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/refreshmv.jsp");
							view.forward(request, response);
						}
						else{
							response.sendRedirect("VeilingController");
						}
					}
					if(request.getParameter("page").equals("mijnbieding")){
						HttpSession session = request.getSession();
						
						if(session.getAttribute("loggedIn") != null){
							GebruikerBean gb = (GebruikerBean) session.getAttribute("loggedIn");
							int userId = gb.getId();
						
							request.setAttribute("mijnBiedingen", b.getMijnBiedingen(userId));
							RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/refreshmb.jsp");
							view.forward(request, response);
						}
						else{
							response.sendRedirect("VeilingController");
						}
					}
				}
			}
			
			else if(item.equals("mijnveiling")){
				HttpSession session = request.getSession();
				
				if(session.getAttribute("loggedIn") != null){
					GebruikerBean gb = (GebruikerBean) session.getAttribute("loggedIn");
					int userId = gb.getId();
					String wijzig = request.getParameter("wijzig");
					
					if(wijzig != null){
						int veilingId = 0;
						try{
						veilingId = Integer.parseInt(wijzig);
						} catch( Exception e){
						response.sendRedirect("VeilingController");
						}
						request.setAttribute("v", d.getMijnVeiling(userId, veilingId));
						RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/wijzig.jsp");
						view.forward(request, response);
					}
					else{						
						request.setAttribute("veilingen", d.getMijnVeilingen(userId));
						RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/mijnVeilingen.jsp");
						view.forward(request, response);
					}
				}
				else{
					response.sendRedirect("VeilingController");
				}
			}
			else if(item.equals("mijnbieding")){
				HttpSession session = request.getSession();
				
				if(session.getAttribute("loggedIn") != null){
					GebruikerBean gb = (GebruikerBean) session.getAttribute("loggedIn");
					int userId = gb.getId();
					request.setAttribute("mijnBiedingen", b.getMijnBiedingen(userId));
				
					RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/MijnBiedingen.jsp");
					view.forward(request, response);
				}
				else{
					response.sendRedirect("VeilingController");
				}
			}
			else{
				int id = 0;
				try{
					id = Integer.parseInt(item);
				} catch( Exception e){
					response.sendRedirect("VeilingController");
				}
				VeilingBean v = d.getVeilingOpId(id);
				if(v != null){
				
					request.setAttribute("biedingen", b.getBiedingen(id));
					request.setAttribute("v", v);
					RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/biedingen.jsp");
					view.forward(request, response);
				}
				else{
					response.sendRedirect("VeilingController");
				}
			}
		}
		else{
			request.setAttribute("veilingen", d.getVeilingen(counter));
			RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/veilingen.jsp");
			view.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
